package pub.wkq.thread;

import java.util.stream.IntStream;

/**
 * 0 ForkJoinPool.commonPool-worker-9 100
 * 8 ForkJoinPool.commonPool-worker-4 100
 * 5 ForkJoinPool.commonPool-worker-8 100
 * 4 ForkJoinPool.commonPool-worker-3 100
 * 9 ForkJoinPool.commonPool-worker-7 100
 * 2 ForkJoinPool.commonPool-worker-1 100
 * 3 ForkJoinPool.commonPool-worker-6 100
 * 6 main 100
 * 1 ForkJoinPool.commonPool-worker-2 100
 * 7 ForkJoinPool.commonPool-worker-5 100
 * 1000
 * @author 王开琦
 */
public class CountingStream {
    public static void main(String[] args) {
        System.out.println(IntStream.range(0, 10)
                .parallel()
                .mapToObj(CountingTask::new)
                .map(countingTask -> {
                    try {
                        return countingTask.call();
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                })
                .reduce(0, Integer::sum)
        );
    }
}
